package com.stock.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.stock.dao.IStockHistoryDataDao;
import com.stock.entity.StockHistoryDataEntity;
import com.stock.enums.StockTypeEnum;
import com.stock.service.IStockHistoryDataService;
import com.stock.vo.StockHistoryDataResultVo;
import com.stock.vo.StockTradeDataResultVo;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

@Service
public class StockHistoryDataService extends ServiceImpl<IStockHistoryDataDao, StockHistoryDataEntity> implements IStockHistoryDataService {

    /**
     * 初始化历史数据
     * @return
     */
    @Override
    public boolean initHistoryStockData() {
        this.remove(new QueryWrapper<>());
        List<StockHistoryDataEntity> historyDataList = Arrays.asList(
                new StockHistoryDataEntity("300245", "天玑科技", new BigDecimal("11.52"), 1, "2024-11-27 10:40:59"),
                new StockHistoryDataEntity("300245", "天玑科技", new BigDecimal("16.28"), 2, "2024-12-04 14:20:34"),

                new StockHistoryDataEntity("002667", "威领股份", new BigDecimal("11.19"), 1, "2012-12-04 14:53:26"),
                new StockHistoryDataEntity("002667", "威领股份", new BigDecimal("10.85"), 2, "2012-12-05 10:28:09"),


                new StockHistoryDataEntity("300968", "格林精密", new BigDecimal("18.52"), 1, "2012-12-05 10:28:29"),
                new StockHistoryDataEntity("300968", "格林精密", new BigDecimal("19.56"), 2, "2012-12-09 10:32:37"),

                new StockHistoryDataEntity("300120", "经纬辉开", new BigDecimal("11.49"), 1, "2012-12-09 10:32:48"),
                new StockHistoryDataEntity("300120", "经纬辉开", new BigDecimal("11.78"), 2, "2012-12-12 14:51:46"),

                new StockHistoryDataEntity("300290", "荣科科技", new BigDecimal("21.67"), 1, "2012-12-13 10:24:00"),
                new StockHistoryDataEntity("300290", "荣科科技", new BigDecimal("21.83"), 2, "2012-12-16 14:55:20"),

                new StockHistoryDataEntity("002542", "中化岩土", new BigDecimal("4.88"), 1, "2012-12-17 10:06:33"),
                new StockHistoryDataEntity("002542", "中化岩土", new BigDecimal("4.72"), 2, "2012-12-18 13:03:20"),

                new StockHistoryDataEntity("300769", "德方纳米", new BigDecimal("42.60"), 1, "2012-12-18 13:03:32"),
                new StockHistoryDataEntity("300769", "德方纳米", new BigDecimal("40.68"), 2, "2012-12-19 10:17:37"),

                new StockHistoryDataEntity("300968", "格林精密", new BigDecimal("18.15"), 1, "2012-12-19 10:19:04"),
                new StockHistoryDataEntity("300968", "格林精密", new BigDecimal("18.55"), 2, "2012-12-23 10:42:18"),

                new StockHistoryDataEntity("300147", "香雪制药", new BigDecimal("12.02"), 1, "2024-12-23 10:43:08")
        );
        return this.saveBatch(historyDataList);
    }

    /**
     * 获取历史数据
     * @return
     */
    @Override
    public List<StockHistoryDataResultVo> getHistoryData() {
        List<StockHistoryDataResultVo> list = baseMapper.getHistoryData();
        if (CollUtil.isNotEmpty(list)) {
            list.stream().forEach(item->{
                item.setTypeStr(StockTypeEnum.getDesc(item.getType()));
            });
        }
        return list;
    }

    /**
     * 获取历史交易数据
     * @return
     */
    @Override
    public List<StockTradeDataResultVo> getHistoryTradeData() {
        return baseMapper.getHistoryTradeData(StockTypeEnum.BUY.getCode(),StockTypeEnum.SELL.getCode());
    }

}
